package com.first.system.service.Impl;

import cn.hutool.core.util.RandomUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.first.common.pojo.AclPermission;
import com.first.common.pojo.AclRolePermission;
import com.first.system.mapper.PermissionMapper;
import com.first.system.mapper.RolePermissionMapper;
import com.first.system.service.PermissionService;
import com.first.system.vo.PermissionInsertVo;
import com.first.system.vo.PermissionVo;
import com.first.system.vo.RolePermissionVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * @program: finrsteducation-parent
 * @description:
 * @author: 张玉森
 * @create: 2023-05-23 20:44
 **/
@Service
public class PermissionServiceImpl implements PermissionService {
    @Resource
    private PermissionMapper permissionMapper;
    @Autowired
    private RolePermissionMapper rolePermissionMapper;

    @Override
    public List<AclPermission> leftMenu(String id) {
        List<AclPermission> list = permissionMapper.selectPermissionList(id);
        List<AclPermission> permissions = new ArrayList<>();
        //1.所有一级菜单
        for (AclPermission aclPermission : list) {
            if (aclPermission.getPid().equals("1")) {
                permissions.add(aclPermission);
            }
        }
        //1.所有子菜单
        for (AclPermission aclPermission : permissions) {
            List<AclPermission> arrayList = this.getChildren(aclPermission.getId(), list);
            aclPermission.setChildren(arrayList);
        }
        return permissions;
    }

    @Override
    public List<AclPermission> findAll() {
        List<AclPermission> permissions = new ArrayList<>();
        QueryWrapper<AclPermission> wrapper=new QueryWrapper<>();
        wrapper.eq("is_deleted",0);
        List<AclPermission> aclPermissionList = permissionMapper.selectList(wrapper);
        //1.所有一级菜单
        for (AclPermission aclPermission : aclPermissionList) {
            if (aclPermission.getPid().equals("1")) {
                permissions.add(aclPermission);
            }
        }
        //1.所有子菜单
        for (AclPermission aclPermission : permissions) {
            List<AclPermission> arrayList = this.getChildren(aclPermission.getId(), aclPermissionList);
            aclPermission.setChildren(arrayList);
        }
        return permissions;
    }

    @Override
    public List<AclRolePermission> findById(String id) {
        QueryWrapper<AclRolePermission> wrapper = new QueryWrapper<>();
        wrapper.eq("is_deleted",0);
        wrapper.eq("role_id", id);
        List<AclRolePermission> rPList = rolePermissionMapper.selectList(wrapper);
        return rPList;
    }

    @Override
    public Page<AclPermission> findPermissionPage(String page, String pageSize, PermissionVo permissionVo) {


        Page<AclPermission> pageAclPermission = new Page<>(Integer.valueOf(page), Integer.valueOf(pageSize));
        QueryWrapper<AclPermission> wrapper = new QueryWrapper<>();
        wrapper.eq("pid", "1");
        wrapper.eq("is_deleted",0);
        if (permissionVo.getName() != null && permissionVo.getName() != "") {
            wrapper.like("name", permissionVo.getName());

        }

        Page<AclPermission> aclPermissionPage = permissionMapper.selectPage(pageAclPermission, wrapper);
        List<AclPermission> list = permissionMapper.selectList(null);
        //1.所有子菜单
        for (AclPermission aclPermission : aclPermissionPage.getRecords()) {
            List<AclPermission> arrayList = this.getChildren(aclPermission.getId(), list);
            aclPermission.setChildren(arrayList);
        }
        return aclPermissionPage;

    }

    @Override
    public Integer updateRolePersission(RolePermissionVo rolePermissionVo) {
        int i = rolePermissionMapper.deletePermissionByRoleid(rolePermissionVo.getRoleId());
        if (i==0){
            System.out.println(i);
            return i;

        }
        List<AclRolePermission> aclRolePermissions=new ArrayList<>();
        String[] permissionIds = rolePermissionVo.getPermissionIds();
        for (String permissionId : permissionIds) {
            AclRolePermission aclRolePermission = new AclRolePermission();
            aclRolePermission.setId(RandomUtil.randomNumbers(19));
            aclRolePermission.setRoleId(rolePermissionVo.getRoleId());
            aclRolePermission.setPermissionId(permissionId);
            aclRolePermission.setIsDeleted(0);
            aclRolePermission.setGmtCreate(LocalDateTime.now());
            aclRolePermission.setGmtModified(LocalDateTime.now());
            aclRolePermissions.add(aclRolePermission);
        }
        System.out.println(aclRolePermissions);
        Integer integer = rolePermissionMapper.insertRolePermission(aclRolePermissions);
        return integer;
    }

    @Override
    public Integer insert(PermissionInsertVo permissionInsertVo) {
        AclPermission aclPermission=new AclPermission();
        aclPermission.setId(RandomUtil.randomNumbers(19));
        aclPermission.setPid(permissionInsertVo.getPid());
        aclPermission.setName(permissionInsertVo.getName());
        aclPermission.setType(permissionInsertVo.getType());
        aclPermission.setPermissionValue(permissionInsertVo.getPermissionValue());
        aclPermission.setPath(permissionInsertVo.getPath());
        aclPermission.setComponent(null);
        aclPermission.setIcon(permissionInsertVo.getIcon());
        aclPermission.setStatus(null);
        aclPermission.setIsDeleted(0);
        aclPermission.setGmtCreate(LocalDateTime.now());
        aclPermission.setGmtModified(LocalDateTime.now());
        return permissionMapper.insert(aclPermission);
    }

    @Override
    public Integer update(AclPermission permission) {
        return permissionMapper.updateById(permission);
    }

    @Override
    public Integer deleteByIds(List<String> permissionIds) {
        Integer deleted= permissionMapper.updateByIds(permissionIds);
       // Integer deleted= permissionMapper.deleteByIds(permissionIds);
        return null;
    }


    private List<AclPermission> getChildren(String id, List<AclPermission> list) {
        List<AclPermission> children = new ArrayList<>();
        for (AclPermission aclPermission : list) {
            if (aclPermission.getPid().equals(id)) {
                children.add(aclPermission);
            }
        }
        for (AclPermission aclPermission1 : children) {
            aclPermission1.setChildren(getChildren(aclPermission1.getId(), list));
        }
        return children;
    }
}
